package com.qumitech.visualnovel.controller.rest;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.qumitech.visualnovel.component.exception.BusinessException;
import com.qumitech.visualnovel.console.excel.ExportExcel;
import com.qumitech.visualnovel.console.excel.StateField;
import com.qumitech.visualnovel.console.excel.State;
import com.qumitech.visualnovel.model.common.BaseRsp;
import com.qumitech.visualnovel.model.common.ResultEnum;
import com.qumitech.visualnovel.model.common.BaseResponse;
import com.qumitech.visualnovel.controller.vo.prop.PropSendExcelVo;
import com.qumitech.visualnovel.model.dao.console.PropertyChange;
import com.qumitech.visualnovel.pubdef.dto.prop.PropChangeRecordAdminDTO;
import com.qumitech.visualnovel.pubdef.dto.prop.PropChangeRecordAdminQueryDTO;
import com.qumitech.visualnovel.service.property.PropertyChangeService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;

@RestController
@RequestMapping("/api/propChange")
public class PropChangeController {

    @Autowired
    PropertyChangeService propertyChangeService;


    @RequestMapping(value = "/changeList",method  = RequestMethod.GET)
    @RequiresPermissions("propChange:view")
    public JSONObject systemLogList(@RequestParam(value="uid",required=false ) Integer accountId,
                                    @RequestParam(value="startTime",required=false) String startTime,
                                    @RequestParam(value="offset", required=false, defaultValue="0") int offset,
                                    @RequestParam(value="limit", required=false, defaultValue="20") int limit,
                                    @RequestParam(value="propertyType", required=false ) String propertyType,
                                    @RequestParam(value="changeType", required=false) String changeType,
                                    @RequestParam(value="orderType", required=false, defaultValue="DESC") String orderType,
                                    @RequestParam(value="orderBy", required=false, defaultValue="change_date") String orderBy){
        Boolean fuzzyModel = false;
        if (propertyType!=null && !propertyType.contains("_")){
            fuzzyModel = true;
        }
        int count=propertyChangeService.getCountByAccount(accountId,startTime,propertyType,changeType, fuzzyModel);
        List<PropertyChange> list=propertyChangeService.getPropertyByAccount(accountId, startTime, propertyType, changeType, offset, limit, orderBy, orderType, fuzzyModel);
        BaseResponse results = new BaseResponse<>(list,count);
        return results;
    }

    @RequestMapping(value = "/propertyIdOptions",method  = RequestMethod.GET)
    public BaseRsp PropertyOptions(){
        return propertyChangeService.getPropertyOptions();
    }


    @PostMapping("")
    public BaseRsp rewardProp(@RequestBody PropChangeRecordAdminDTO dto) {
        return propertyChangeService.rewardProp(dto);
    }

    @GetMapping("/record")
    @ExportExcel(itemCls = PropSendExcelVo.class,
            stateFields = {@StateField(key="status", states = {@State(state="1", display = "成功"), @State(state="0", display = "失败")})}
    )
    public BaseRsp getRecord(PropChangeRecordAdminQueryDTO queryAdminDTO) {
        return propertyChangeService.listPropChangeRecord(queryAdminDTO);
    }

    @RequestMapping(value = "/reasonIdOptions",method  = RequestMethod.GET)
    public JSONObject ReasonIdOptions(){
        List<HashMap<String,Object>> list=propertyChangeService.getReasonOptions();
        BaseResponse results = new BaseResponse(list);
        return results;
    }

    @GetMapping("/card/options")
    public BaseRsp getOptions() {
        return propertyChangeService.listCardOptions();
    }

}
